/**************************************************************************************
*  VmTests.java
*  simpleRTJ regression tests.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*       RTJ Computing
**************************************************************************************/
import lpc2xxx.hw.*;

class VmTests extends Thread
{
    static void main(String[] args)
    {
        new VmTests().start();
    }

    public void run()
    {
        boolean forever = false;
        while (true)
        {
            if (forever)
            {
                if (getKey() > 0)
                    forever = false;
            }
            else
            {
                // transmit a welcome message
                Console.println("\r\nJava VM Testing Suite.\r\n");
                Console.println("Press Enter for single test or 'C' for continuous test.");
                Console.println("(press ESC key during test to stop)");
                while (true)
                {
                    int ch = getKey();
                    if (ch == 0x1B)
                        return;
                    if (ch == 0x0D)
                        break;
                    if (ch == 'c' || ch == 'C')
                    {
                        forever = true;
                        break;
                    }
                }
                Console.println("\r\n");
            }

            int failed = 0;
            try
            {
                Console.print("Boolean.. ");
                TestBoolean.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Byte.. ");
                TestByte.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Short.. ");
                TestShort.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Integer.. ");
                TestInt.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Float.. ");
                TestFloat.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Control.. ");
                TestControl.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Exception.. ");
                TestException.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Finally.. ");
                TestFinally.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Instance.. ");
                TestInstance.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Initialisation.. ");
                TestInitialisation.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Object Initialisation.. ");
                TestObjectInitialisation.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("Lookup switch.. ");
                TestLookupSwitch.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }

            try
            {
                Console.print("Object synchronization.. ");
                TestSynch.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }

            try
            {
                Console.print("Interface.. ");
                TestInterface.test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }
            try
            {
                Console.print("instanceof/checkcast.. ");
                TestCastInstanceOf iof = new TestCastInstanceOf();
                iof.test();
                Console.println("OK");
            }
            catch (ClassCastException e)
            {
                failed++;
                Console.println("Unhandled ClassCastException\r\n");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }

            try
            {
                Console.print("Cloning.. ");
                new TestClone().test();
                Console.println("OK");
            }
            catch (Exception e)
            {
                failed++;
                Console.println("Fail: "+e.getMessage()+"\r\n");
            }

            if (failed == 0)
                Console.println("\r\nAll tests passed.\r\n");
            else
            {
                Console.println("\r\n"+failed+" test(s) have failed.\r\n");
                forever = false;
            }
        }
    }

    static void printString(String s)
    {
        Console.print(s);
    }

    static char getKey()
    {
        return Console.getKey();
    }
}
